<?php

namespace app\repository;

use app\popo\model\AddressBook;
use think\helper\Arr;

class AddressBookRepository
{
    /**
     * 根据条件查询地址列表
     * @param array $conditions
     * @return array
     */
    public function list(array $conditions): array
    {
        $query = AddressBook::query();
        
        foreach ($conditions as $field => $value) {
            if ($value !== null) {
                $query->where($field, $value);
            }
        }
        
        return $query->orderBy('is_default', 'desc')
                    ->orderBy('id', 'desc')
                    ->get()
                    ->map(function (AddressBook $addressBook) {
                        return $this->format($addressBook);
                    })
                    ->toArray();
    }

    /**
     * 根据ID查询地址
     * @param int $id
     * @return AddressBook|null
     */
    public function getById(int $id): ?AddressBook
    {
        return AddressBook::find($id);
    }

    /**
     * 保存地址
     * @param array $data
     * @return AddressBook
     */
    public function save(array $data): AddressBook
    {
        $addressBook = new AddressBook();
        foreach ($data as $key => $value) {
            if (in_array($key, $addressBook->getFillable()) && $value !== null) {
                $addressBook->{$key} = $value;
            }
        }
        $addressBook->save();
        return $addressBook;
    }

    /**
     * 更新地址
     * @param AddressBook $addressBook
     * @param array $data
     * @return void
     */
    public function update(AddressBook $addressBook, array $data): void
    {
        foreach ($data as $key => $value) {
            if (in_array($key, $addressBook->getFillable()) && $value !== null) {
                $addressBook->{$key} = $value;
            }
        }
        $addressBook->save();
    }

    /**
     * 删除地址
     * @param AddressBook $addressBook
     * @return void
     */
    public function delete(AddressBook $addressBook): void
    {
        $addressBook->delete();
    }

    /**
     * 将用户的所有地址设为非默认
     * @param int $userId
     * @return void
     */
    public function clearDefaultByUserId(int $userId): void
    {
        AddressBook::where('user_id', $userId)
                   ->where('is_default', 1)
                   ->update(['is_default' => 0]);
    }

    /**
     * 设置地址为默认
     * @param AddressBook $addressBook
     * @return void
     */
    public function setAsDefault(AddressBook $addressBook): void
    {
        $addressBook->is_default = 1;
        $addressBook->save();
    }

    /**
     * 检查地址是否属于指定用户
     * @param int $id
     * @param int $userId
     * @return bool
     */
    public function belongsToUser(int $id, int $userId): bool
    {
        return AddressBook::where('id', $id)
                         ->where('user_id', $userId)
                         ->exists();
    }

    public function format(AddressBook $addressBook): array
    {
        return [
            'id' => $addressBook->id,
            'userId' => $addressBook->user_id,
            'consignee' => $addressBook->consignee,
            'phone' => $addressBook->phone,
            'sex' => $addressBook->sex,
            'provinceCode' => $addressBook->province_code,
            'provinceName' => $addressBook->province_name,
            'cityCode' => $addressBook->city_code,
            'cityName' => $addressBook->city_name,
            'districtCode' => $addressBook->district_code,
            'districtName' => $addressBook->district_name,
            'detail' => $addressBook->detail,
            'label' => $addressBook->label,
            'isDefault' => (int)$addressBook->is_default,
        ];
    }
}